import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class UDPServer{
	JFrame frame;
	JPanel panel;
	JButton button1,button2;
	JTextArea area;
	JScrollPane pane;
	Thread thread;
	DatagramSocket socket;

	public static void main(String[] args) 
	{
		UDPServer u = new UDPServer();
	}

	public UDPServer()
	{
		frame = new JFrame("Text Server");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setUndecorated(true);
		frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
		panel = new JPanel();
		panel.setLayout(null);
		area = new JTextArea();
		area.setEditable(false);
		button1 = new JButton("Start");
		button1.setBounds(210, 10, 75, 40);
		button1.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent ae)
			{
				new StartThread();
			}
		});
		panel.add(button1);
		button2 = new JButton("Stop");
		button2.setBounds(300, 10, 75, 40);
		button2.addActionListener(new ActionListener()
		{
			public void actionPerformed (ActionEvent ae)
			{
				thread.interrupted();
				socket.close();
				area.append("Server is stopped\n");
				button1.setEnabled(true);
				button2.setEnabled(false);
			}
		});
		button2.setEnabled(false);
		panel.add(button2);
		pane = new JScrollPane(area);
		pane.setBounds(10, 60, 365, 250);
		panel.add(pane);
		frame.add(panel);
		frame.setSize(400, 400);
		frame.setVisible(true);
	}
	public class StartThread implements Runnable
	{
		StartThread()
		{
			thread = new Thread(this);
			thread.start();
			button1.setEnabled(false);
			button2.setEnabled(true);
		}
		public void run()
		{
			try{
				int port = 55555;
				try{
					socket = new DatagramSocket(port);
					area.append("Server is started\n");
					while(true)
					{
						try{
							byte[] buffer = new byte[110];
							//Receive request from client
							DatagramPacket packet = new DatagramPacket(buffer, buffer.length );
							socket.receive(packet);
							InetAddress client = packet.getAddress();
							int client_port = packet.getPort();
							//area.append(" Received " + new String(buffer)+" from "+client);

							String[] message = new String[12];

							message[0] = "MAC:      ";
							message[1] = "Channel:  ";
							message[2] = "RSSI:     ";
							message[3] = "Port:     ";
							message[4] = "RTC V.:   ";
							message[5] = "Bat.Vol.: ";
							message[6] = "GPIO Pin: ";
							message[10] = "Boot t.:  ";
							message[11] = "Sens.Vol: ";

							byte[] time = new byte[13];
							byte[] version = new byte[26];
							byte[] device = new byte[32];

							for(int i=0; i<buffer.length; i++)
							{
								if(i>= 0 && i<=  5) message[0]       += Integer.toString((buffer[i] & 0xff ) + 0x100, 16).substring(1);
								else if(i>= 6 && i<=  6) message[1]  += Integer.toString((buffer[i] & 0xff ) + 0x100, 16).substring(1);
								else if(i>= 8 && i<=  9) message[3]  += Integer.toString((buffer[i] & 0xff ) + 0x100, 16).substring(1);
								else if(i>=10 && i<= 13) message[4]  += Integer.toString((buffer[i] & 0xff ) + 0x100, 16).substring(1);
								else if(i>=14 && i<= 15) message[5]  += Integer.toString((buffer[i] & 0xff ) + 0x100).substring(1);
								else if(i>=16 && i<= 17) message[6]  += Integer.toString((buffer[i] & 0xff ) + 0x100, 16).substring(1);
								else if(i>=18 && i<= 30) time[i-18] = buffer[i];
								else if(i>=32 && i<= 57) version[i-32] = buffer[i];
								else if(i>=60 && i<= 91) device[i-60] = buffer[i];
								else if(i>=92 && i<= 93) message[10] += Integer.toString((buffer[i] & 0xff ) + 0x100, 16).substring(1);
								else if(i>=94 && i<=109) message[11] += Integer.toString((buffer[i] & 0xff ) + 0x100, 16).substring(1);
							}
							message[7] = "Time:     " + new String(time);
							message[8] = "Version:  " + new String(version);
							message[9] = "Device:   " + new String(device);

							for(int i=0; i<message.length; i++)
							{
								area.append(message[i] + "\n");
							}
							area.append("from: " + client + "\n");
							area.append("----------------------------------------------------------\n");
						}
						catch(UnknownHostException ue){}
					}
				}
				catch(java.net.BindException b){}
			}
			catch (IOException e)
			{
				System.err.println(e);
			}
		}

		public String[] splitBuffer(byte[] buffer)
		{
			String[] message = new String[12];
			byte[] time = new byte[13];
			byte[] version = new byte[26];
			byte[] device = new byte[32];

			for(int i=0; i<buffer.length; i++)
			{
				if(i>= 0 && i<=  5) message[0]  = "MAC:      " + Integer.toString((buffer[i] & 0xff ) + 0x100, 16).substring(1);
				else if(i>= 6 && i<=  6) message[1]  = "Channel:  " + Integer.toString((buffer[i] & 0xff ) + 0x100, 16).substring(1);
				else if(i>= 7 && i<=  7) message[2]  = "RSSI:     " + Integer.toString((buffer[i] & 0xff ) + 0x100, 16).substring(1);
				else if(i>= 8 && i<=  9) message[3]  = "Port:     " + Integer.toString((buffer[i] & 0xff ) + 0x100, 16).substring(1);
				else if(i>=10 && i<= 13) message[4]  = "RTC V.:   " + Integer.toString((buffer[i] & 0xff ) + 0x100, 16).substring(1);
				else if(i>=14 && i<= 15) message[5]  = "Bat.Vol.: " + Integer.toString((buffer[i] & 0xff ) + 0x100, 16).substring(1);
				else if(i>=16 && i<= 17) message[6]  = "GPIO Pin: " + Integer.toString((buffer[i] & 0xff ) + 0x100, 16).substring(1);
				else if(i>=18 && i<= 31) time[i-18] = buffer[i];
				else if(i>=32 && i<= 59) version[i-32] = buffer[i];
				else if(i>=60 && i<= 91) device[i-60] = buffer[i];
				else if(i>=92 && i<= 93) message[10] = "Boot t.:  " + Integer.toString((buffer[i] & 0xff ) + 0x100, 16).substring(1);
				else if(i>=94 && i<=109) message[11] = "Sens.Vol: " + Integer.toString((buffer[i] & 0xff ) + 0x100, 16).substring(1);
			}
			message[7] = "Time:     " + new String(time);
			message[8] = "Version:  " + new String(version);
			message[9] = "Device:   " + new String(device);


			return message;
		}
	}
}